<template>
    <div class="article-ranking-container">
        <el-card class="header">
            <div class="dynamic-box">
                <span>动态展示</span>
                <el-checkbox-group v-model="selectDynamic">
                    <el-checkbox v-for="item in dynamiData" :label="item.label" :key="item"
                        :disabled="item.label == '标题'" />
                </el-checkbox-group>
            </div>
        </el-card>
        <el-card>
            <el-table :data="articleList" v-loading="loading" border ref="tableRef">
                <el-table-column v-for="item in tableColumns" :prop="item.prop" :key="item.prop" :label="item.label">
                    <template v-if="item.prop == 'date'" #default="{ row }">
                        {{ formatTime(row.date) }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right">
                    <template #default="{row}">
                        <el-button type="primary" size="small" @click="openDetail(row)">展示详情</el-button>
                        <el-button type="danger" size="small" @click="articleDeleteFn(row._id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-model:current-page="queryParams.currentPage" v-model:page-size="queryParams.pageSize"
                :page-sizes="[5, 10, 50]" layout="sizes, prev, pager, next,total,jumper" prev-text="上一页" next-text="下一页"
                :total="total" class="pagination" />
        </el-card>
    </div>
    <articleDetail ref="DetailRef"></articleDetail>
</template>

<script setup>
import { ref,watch,onMounted,onActivated } from 'vue'
import { selectDynamic, tableColumns, dynamiData } from './dynamic/index'
import { getArticleList,deleteArticle } from '@/apis/article'
import { formatTime } from '@/utils/formmatTime'
import {tableRef,initSorTable} from './sortable/index'
import articleDetail from './components/articleDetai.vue'
import { ElMessage } from 'element-plus'

onMounted(()=>{
    initSorTable(articleList,getArticleListFn)
})
const queryParams = ref({
    pageSize: 5,
    currentPage: 1
})
watch(queryParams.value,()=>{
    getArticleListFn()
})
const articleList = ref([])
const total = ref(0)
const loading = ref(false)
const getArticleListFn = async () => {
    loading.value = true
    const { data } = await getArticleList(queryParams.value)
    articleList.value = data.data
    total.value = data.total
    setTimeout(() => { loading.value = false }, 500)
}
getArticleListFn()

// 打开文章详情
const DetailRef = ref({})
const openDetail = (row)=>{
    DetailRef.value.open(row)
}

// 删除文章
const articleDeleteFn = async (_id)=>{
    const {data} = await deleteArticle({_id})
    if (!data.code){
        ElMessage.success('删除成功')
        getArticleListFn()
    }
}
onActivated(()=>{
    getArticleListFn()
})



</script>


<style lang="scss" scoped>
:deep(.el-card_body) {
    overflow: hidden;
}

.article-ranking-container {
    .header {
        margin-bottom: 20px;

        .dynamic-box {
            display: flex;
            align-items: center;
            .title {
                margin-right: 20px;
                font-size: 14px;
                font-weight: bold;
            }
        }
    }

    :deep(.el-table_row) {
        cursor: pointer;
        height: 75px !important;
    }
    // 这个是表格拖拽时的样式
:deep(.sortable-ghost) {
    opacity: .6;
    color: #fff !important;
    background: #304156 !important;
}


}
</style>